package com.zenithmind.document.pojo.vo;

import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;

/**
 * 文档收藏VO
 * 遵循单一职责原则：专注于收藏数据展示
 * 遵循开闭原则：支持字段的扩展
 * 
 * @author ZenithMind Team
 * @since 2025-01-09
 */
@Data
@Schema(description = "文档收藏VO")
public class DocumentFavoriteVO {

    @Schema(description = "收藏ID", example = "fav123456")
    private String id;

    @Schema(description = "文档ID", example = "doc123456")
    private String documentId;

    @Schema(description = "文档标题", example = "项目需求文档")
    private String documentTitle;

    @Schema(description = "文档类型", example = "PDF")
    private String documentType;

    @Schema(description = "文档大小", example = "1024000")
    private Long documentSize;

    @Schema(description = "文档大小描述", example = "1MB")
    private String documentSizeDesc;

    @Schema(description = "文档分类", example = "技术文档")
    private String documentCategory;

    @Schema(description = "文档作者", example = "张三")
    private String documentAuthor;

    @Schema(description = "文档创建时间", example = "2025-01-09T10:00:00")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime documentCreateTime;

    @Schema(description = "用户ID", example = "user123456")
    private String userId;

    @Schema(description = "用户名", example = "李四")
    private String userName;

    @Schema(description = "用户昵称", example = "小李")
    private String userNickname;

    @Schema(description = "用户头像", example = "https://example.com/avatar.jpg")
    private String userAvatar;

    @Schema(description = "收藏夹ID", example = "folder123456")
    private String folderId;

    @Schema(description = "收藏夹名称", example = "我的收藏")
    private String folderName;

    @Schema(description = "收藏夹描述", example = "重要文档收藏夹")
    private String folderDescription;

    @Schema(description = "收藏夹颜色", example = "#FF5722")
    private String folderColor;

    @Schema(description = "收藏夹图标", example = "fa-star")
    private String folderIcon;

    @Schema(description = "标签", example = "重要,学习,参考")
    private String tags;

    @Schema(description = "标签数组", example = "[\"重要\", \"学习\", \"参考\"]")
    private String[] tagArray;

    @Schema(description = "备注", example = "这是一个重要的参考文档")
    private String remarks;

    @Schema(description = "是否公开", example = "true")
    private Boolean isPublic;

    @Schema(description = "排序权重", example = "100")
    private Integer sortWeight;

    @Schema(description = "状态", example = "ACTIVE")
    private String status;

    @Schema(description = "状态描述", example = "活跃")
    private String statusDesc;

    @Schema(description = "收藏时间", example = "2025-01-09T10:30:00")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime favoriteTime;

    @Schema(description = "收藏时间描述", example = "2小时前")
    private String favoriteTimeDesc;

    @Schema(description = "最后访问时间", example = "2025-01-09T15:30:00")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime lastAccessTime;

    @Schema(description = "最后访问时间描述", example = "30分钟前")
    private String lastAccessTimeDesc;

    @Schema(description = "访问次数", example = "15")
    private Integer accessCount;

    @Schema(description = "创建时间", example = "2025-01-09T10:30:00")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime createTime;

    @Schema(description = "更新时间", example = "2025-01-09T10:35:00")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime updateTime;

    @Schema(description = "扩展属性", example = "{\"priority\": \"high\"}")
    private String extendedProperties;

    /**
     * 状态名称
     */
    @Schema(description = "状态名称")
    private String statusName;

    /**
     * 是否活跃
     */
    @Schema(description = "是否活跃")
    private Boolean isActive;

    /**
     * 是否默认收藏夹
     */
    @Schema(description = "是否默认收藏夹")
    private Boolean isDefaultFolder;

    /**
     * 收藏天数
     */
    @Schema(description = "收藏天数")
    private Long favoriteDays;

    /**
     * 是否最近收藏
     */
    @Schema(description = "是否最近收藏")
    private Boolean isRecentFavorite;

    /**
     * 获取状态描述
     */
    public String getStatusDescription() {
        if (status == null) return "";
        
        switch (status.toUpperCase()) {
            case "ACTIVE": return "活跃";
            case "INACTIVE": return "非活跃";
            case "DELETED": return "已删除";
            default: return status;
        }
    }

    /**
     * 获取文档大小描述
     */
    public String getDocumentSizeDescription() {
        if (documentSize == null || documentSize <= 0) return "0B";
        
        String[] units = {"B", "KB", "MB", "GB", "TB"};
        int unitIndex = 0;
        double size = documentSize.doubleValue();
        
        while (size >= 1024 && unitIndex < units.length - 1) {
            size /= 1024;
            unitIndex++;
        }
        
        return String.format("%.2f%s", size, units[unitIndex]);
    }

    /**
     * 获取时间描述
     */
    public String getTimeDescription(LocalDateTime time) {
        if (time == null) return "";
        
        LocalDateTime now = LocalDateTime.now();
        long minutes = ChronoUnit.MINUTES.between(time, now);
        
        if (minutes < 1) return "刚刚";
        else if (minutes < 60) return minutes + "分钟前";
        else if (minutes < 1440) return (minutes / 60) + "小时前";
        else if (minutes < 43200) return (minutes / 1440) + "天前";
        else return time.toLocalDate().toString();
    }

    /**
     * 获取标签数组
     */
    public String[] getTagArray() {
        if (tags == null || tags.trim().isEmpty()) {
            return new String[0];
        }
        return tags.split(",");
    }

    /**
     * 是否为公开收藏
     */
    public boolean isPublicFavorite() {
        return isPublic != null && isPublic;
    }

    /**
     * 是否为私有收藏
     */
    public boolean isPrivateFavorite() {
        return isPublic == null || !isPublic;
    }

    /**
     * 是否为活跃状态
     */
    public boolean isActive() {
        return "ACTIVE".equalsIgnoreCase(status);
    }

    /**
     * 是否已删除
     */
    public boolean isDeleted() {
        return "DELETED".equalsIgnoreCase(status);
    }

    /**
     * 是否有标签
     */
    public boolean hasTags() {
        return tags != null && !tags.trim().isEmpty();
    }

    /**
     * 是否有备注
     */
    public boolean hasRemarks() {
        return remarks != null && !remarks.trim().isEmpty();
    }

    /**
     * 是否最近收藏（24小时内）
     */
    public boolean isRecentlyFavorited() {
        if (favoriteTime == null) return false;
        return ChronoUnit.HOURS.between(favoriteTime, LocalDateTime.now()) <= 24;
    }

    /**
     * 是否最近访问（24小时内）
     */
    public boolean isRecentlyAccessed() {
        if (lastAccessTime == null) return false;
        return ChronoUnit.HOURS.between(lastAccessTime, LocalDateTime.now()) <= 24;
    }

    /**
     * 是否为热门收藏（访问次数多）
     */
    public boolean isPopular() {
        return accessCount != null && accessCount >= 10;
    }

    /**
     * 获取收藏天数
     */
    public long getFavoriteDays() {
        if (favoriteTime == null) return 0;
        return ChronoUnit.DAYS.between(favoriteTime, LocalDateTime.now());
    }

    /**
     * 计算活跃度分数
     */
    public double calculateActivityScore() {
        double score = 0.0;
        
        // 访问次数权重 40%
        if (accessCount != null) {
            score += Math.min(accessCount / 50.0, 1.0) * 0.4;
        }
        
        // 最近访问权重 30%
        if (lastAccessTime != null) {
            long daysSinceLastAccess = ChronoUnit.DAYS.between(lastAccessTime, LocalDateTime.now());
            score += Math.max(0, (30 - daysSinceLastAccess) / 30.0) * 0.3;
        }
        
        // 收藏时长权重 20%
        long favoriteDays = getFavoriteDays();
        if (favoriteDays > 0) {
            score += Math.min(favoriteDays / 365.0, 1.0) * 0.2;
        }
        
        // 是否有标签和备注权重 10%
        if (hasTags() || hasRemarks()) {
            score += 0.1;
        }
        
        return Math.min(score, 1.0);
    }

    /**
     * 获取活跃度等级
     */
    public String getActivityLevel() {
        double score = calculateActivityScore();
        
        if (score >= 0.8) return "非常活跃";
        else if (score >= 0.6) return "活跃";
        else if (score >= 0.4) return "一般";
        else if (score >= 0.2) return "较少";
        else return "很少";
    }

    /**
     * 是否为重要收藏
     */
    public boolean isImportant() {
        if (tags != null) {
            String lowerTags = tags.toLowerCase();
            return lowerTags.contains("重要") || lowerTags.contains("important") || 
                   lowerTags.contains("urgent") || lowerTags.contains("紧急");
        }
        return false;
    }

    /**
     * 获取收藏优先级
     */
    public String getPriority() {
        if (isImportant()) return "高";
        if (isPopular()) return "中";
        return "低";
    }
}
